import axios from '@/axios';
import store from '@/store';

export default {
  isLogin() {
    const token = localStorage.getItem('token');
    const loginSysUserVo = localStorage.getItem('loginSysUserVo');
    const permissionCodes = localStorage.getItem('permissionCodes');
    if (token && loginSysUserVo && permissionCodes) {
      return true;
    }
    return false;
  },
  async login(params) {
    const config = { disableLoading: true };
    const { loginSysUserVo, token } = await axios.post('/api/login', params, config);
    localStorage.setItem('token', token);
    localStorage.setItem('loginSysUserVo', JSON.stringify(loginSysUserVo));
    localStorage.setItem('permissionCodes', JSON.stringify(loginSysUserVo.permissionCodes));
    store.dispatch('updateLoginSysUserVo', loginSysUserVo);
    return loginSysUserVo;
  },
  async logout() {
    localStorage.removeItem('token');
    localStorage.removeItem('loginSysUserVo');
    localStorage.removeItem('permissionCodes');
    store.dispatch('updateLoginSysUserVo', null);
  },
};
